package k8s_agent

import (
	devcontainer_dto "code.gitea.io/gitea/modules/devstar_devcontainer/k8s_agent/dto"
	devcontainer_errors "code.gitea.io/gitea/modules/devstar_devcontainer/k8s_agent/errors"
	"code.gitea.io/gitea/modules/log"
	"context"
	"fmt"
	dynamic_client "k8s.io/client-go/dynamic"
)

func DeleteDevcontainer(ctx *context.Context, client dynamic_client.Interface, opts *devcontainer_dto.DeleteDevcontainerOptions) error {
	if ctx == nil || opts == nil || len(opts.Namespace) == 0 || len(opts.Name) == 0 {
		return devcontainer_errors.ErrIllegalDevcontainerParameters{
			FieldList: []string{"ctx", "opts", "opts.Name", "opts.Namespace"},
			Message:   "cannot be nil",
		}
	}

	err := client.Resource(groupVersionResource).Namespace(opts.Namespace).Delete(*ctx, opts.Name, opts.DeleteOptions)
	if err != nil {
		log.Warn("Failed to delete DevcontainerApp '%s' in namespace '%s': %s", opts.Name, opts.Namespace, err.Error())
		return devcontainer_errors.ErrOperateDevcontainer{
			Action:  fmt.Sprintf("delete devcontainer '%s' in namespace '%s'", opts.Name, opts.Namespace),
			Message: err.Error(),
		}
	}
	return nil
}
